<?php

$incidentFiles = array("incident.json.121112_122347",
"incident.json.121114_115503",
"incident.json.121116_095548",
"incident.json.121118_070948",
"incident.json.121120_131218",
"incident.json.121122_054202",
"incident.json.121123_172716",
"incident.json.121125_172402",
"incident.json.121127_105902",
"incident.json.121128_203003",
"incident.json.121130_145901");

$base_url = "http://localhost/~mingxuanli/syd-traffic/server_program/server.php";
$incident_url = "../traffic-data/Incidents/";
$f3_url = "../traffic-data/F3/";
$m4_url = "../traffic-data/M4/";
$m7_url = "../traffic-data/M7/";
$filtered_incident_url = "../traffic-data/filtered/Incidents/";
$filtered_f3_url = "../traffic-data/filtered/F3/";
$filtered_m4_url = "../traffic-data/filtered/M4/";
$filtered_m7_url = "../traffic-data/filtered/M7/";


$oneHour = 3600;

	function fileToTime($filename){
		$strArr = explode(".",$filename);
		return makeTime(end($strArr));
	}
	
	function makeTime($timeString){
		$strArr = explode("_",$timeString);
		$date = (int) $strArr[0];
		$time = (int) $strArr[1];
		$hour = floor($time/10000);
		$minute = floor(($time-$hour*10000)/100);
		$second = ($time-$hour*10000-$minute*100);
		$year = floor($date/10000);
		$month = floor(($date-$year*10000)/100);
		$day = ($date-$year*10000-$month*100);
		return mktime($hour,$minute,$second,$month,$day,$year);
	}
	
	function searchFilesOnTime($url,$startTime,$endTime){
		$dir = opendir($url);
		if(!$dir){
			return -1;
		}
		$retArr = array();
		while (($file = readdir($dir)) !== false) {
        	if ($file[0] == '.'){
        		continue; 
			}
        	if (is_dir($url.$file)){       
        	                     
        	}else {
        		$fileTime = fileToTime($file);
        		if($fileTime >= $startTime && $fileTime <= $endTime){
            		array_push($retArr,$file);
        		}
        	}
   		}   
    	closedir($dir);
     	return $retArr;
	}

	
	date_default_timezone_set('Australia/Sydney');
	
	foreach($incidentFiles as $incidentFile){
		$time = fileToTime($incidentFile);
		$arrayFiles = searchFilesOnTime($incident_url,$time,$time+$oneHour);
		foreach($arrayFiles as $file){
			echo $incident_url.$file."<br>";
			echo $filtered_incident_url.$file."<br>";
			$data = file_get_contents($incident_url.$file);
			$handle = fopen($filtered_incident_url.$file, "x+");
			fwrite($handle, $data);
			fclose($handle);
		}
		$arrayFiles_f3 = searchFilesOnTime($f3_url,$time,$time+$oneHour);
		foreach($arrayFiles_f3 as $file){
			echo $f3_url.$file."<br>";
			echo $filtered_f3_url.$file."<br>";
			$data = file_get_contents($f3_url.$file);
			$handle = fopen($filtered_f3_url.$file, "x+");
			fwrite($handle, $data);
			fclose($handle);
		}
		$arrayFiles_m4 = searchFilesOnTime($m4_url,$time,$time+$oneHour);
		foreach($arrayFiles_m4 as $file){
			echo $m4_url.$file."<br>";
			echo $filtered_m4_url.$file."<br>";
			$data = file_get_contents($m4_url.$file);
			$handle = fopen($filtered_m4_url.$file, "x+");
			fwrite($handle, $data);
			fclose($handle);
		}
		$arrayFiles_m7 = searchFilesOnTime($m7_url,$time,$time+$oneHour);
		foreach($arrayFiles_m7 as $file){
			echo $m7_url.$file."<br>";
			echo $filtered_m7_url.$file."<br>";
			$data = file_get_contents($m7_url.$file);
			$handle = fopen($filtered_m7_url.$file, "x+");
			fwrite($handle, $data);
			fclose($handle);
		}
	}

?>